package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    //菜品列表分页查询
    @GetMapping("dish/page")
    public ResultInfo findByPage(
            //使用@RequestParam注解可以给请求路径中的参数起别名,设置默认值
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name
    ) {
        //1.调用Service分页查询
        Page<Dish> page = dishService.findByPage(name, pageNum, pageSize);

        //2.返回结果
        return ResultInfo.success(page);
    }

    //新增
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        dishService.save(dish);
        return ResultInfo.success(null);
    }

    //回显菜品信息
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        Dish dish = dishService.findById(id);
        return ResultInfo.success(dish);
    }

    //修改菜品信息
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish) {
        dishService.update(dish);
        return ResultInfo.success(null);
    }

    //修改商品状态 /dish/status/0?ids=1531954494430588930
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status") Integer status, Long[] ids) {
        dishService.updateStatus(status, ids);
        return ResultInfo.success(null);
    }

    //批量删除
    @DeleteMapping("/dish")
    public ResultInfo deleteById(Long[] ids) {
        ResultInfo resultInfo = dishService.deleteById(ids);
        return resultInfo;
    }

    //新增套餐界面回显菜品/模糊查询菜品
    @GetMapping("/dish/list")
    public ResultInfo findBycategoryId(Long categoryId,String name) {
        List<Dish> dishList = dishService.findBycategoryId(categoryId,name);
        return ResultInfo.success(dishList);
    }


}
